package com.five.taotao.search;

import android.content.Intent;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.view.inputmethod.EditorInfo;
import android.widget.EditText;
import android.widget.TextView;

import com.five.taotao.Adapter.VPTabAdapter;
import com.five.taotao.R;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class SearchResultActivity extends AppCompatActivity {


    @BindView(R.id.tabs)
    TabLayout tabs;
    @BindView(R.id.vp)
    ViewPager vp;

    @BindView(R.id.search)
    EditText editText;

    @OnClick(R.id.message_search)
    public void search(View v) {
        String tmp = editText.getText().toString();
        fragment1.refresh(tmp);
    }

    @OnClick(R.id.back)
    public void back(View v) {
        finish();
    }

    private VPTabAdapter adapter;
    private List<Fragment> mData;
    private List<String> strings;

    private SearchResultFragment fragment1;
    private SearchResultFragment fragment2;
    private SearchResultFragment fragment3;
    private SearchResultFragment fragment4;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_search);
        ButterKnife.bind(this);

        int productType = getIntent().getIntExtra("productType",0);
        String search_name = getIntent().getStringExtra("search_name");
        editText.setText(search_name);

        tabs.setTabMode(TabLayout.MODE_FIXED);//设置可以滑动
        tabs.setTabGravity(TabLayout.GRAVITY_FILL);

        initData(productType,search_name);//初始化数据

        //适配器
        adapter = new
                VPTabAdapter(getSupportFragmentManager(), mData, strings);
        vp.setAdapter(adapter);
        //关联tabs 和 ViewPage
        tabs.setupWithViewPager(vp);



//        vp.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
//            @Override
//            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
//
//            }
//
//            @Override
//            public void onPageSelected(int position) {
//                tmpPosition = position;
//            }
//
//            @Override
//            public void onPageScrollStateChanged(int state) {
//
//            }
//        });
        editText.setOnEditorActionListener(new TextView.OnEditorActionListener() {
            @Override
            public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
                if (actionId == EditorInfo.IME_ACTION_SEARCH || (event != null && event.getKeyCode() == KeyEvent.KEYCODE_ENTER)) {
                    String tmp = editText.getText().toString();
                    fragment1.refresh(tmp);
                    return true;
                }
                return true;
            }
        });
    }

    private void initData(int productType,String search_name) {
        mData = new ArrayList<>();
        strings = new ArrayList<>();
        strings.add("人气");
        strings.add("销量");
        strings.add("最新");
        strings.add("价格");

        // 通过序列化传递数据
        // bundle.putSerializable("newsType", types.get(i));
        //  通过Parcelable传递数据
        // bundle.putParcelable("newsType_parcelable",types.get(i));
        fragment1 = new SearchResultFragment();
        Bundle bundle1 = new Bundle();//通过Bundle 携带数据 给 碎片onCreateView()方法使用
        bundle1.putInt("productType", productType);
        bundle1.putString("search_name", search_name);
        bundle1.putInt("id",1);
        fragment1.setArguments(bundle1);
        mData.add(fragment1);

        fragment2 = new SearchResultFragment();
        Bundle bundle2 = new Bundle();//通过Bundle 携带数据 给 碎片onCreateView()方法使用
        bundle2.putInt("productType", productType);
        bundle2.putString("search_name", search_name);
        bundle2.putInt("id", 2);
        fragment2.setArguments(bundle2);
        mData.add(fragment2);

        fragment3 = new SearchResultFragment();
        Bundle bundle3 = new Bundle();//通过Bundle 携带数据 给 碎片onCreateView()方法使用
        bundle3.putInt("productType", productType);
        bundle3.putString("search_name", search_name);
        bundle3.putInt("id", 3);
        fragment3.setArguments(bundle3);
        mData.add(fragment3);

        fragment4 = new SearchResultFragment();
        Bundle bundle4 = new Bundle();//通过Bundle 携带数据 给 碎片onCreateView()方法使用
        bundle4.putInt("productType", productType);
        bundle4.putString("search_name", search_name);
        bundle4.putInt("id",4);
        fragment4.setArguments(bundle4);
        mData.add(fragment4);

    }
}
